using System;
using System.DHTML;
using Ext;

namespace SampleScripts.messagebox {
    public class MessageBoxScript {
        public static void main(Dictionary args) {
			ExtClass.onReady(new AnonymousDelegate(delegate() { new MessageBoxScript().init(); }));
        }

		public void init()
		{
			ExtClass.get("mb1").on("click", new AnonymousDelegate(delegate
				{
					MessageBox.confirm("Confirm", "Are you sure you want to do that?", new MessageBoxResponseDelegate(ShowResult));
				}));

			ExtClass.get("mb2").on("click", new AnonymousDelegate(delegate
			{
				MessageBox.prompt("Name", "Please enter your name:", new MessageBoxResponseDelegate(ShowResultText));
			}));

			ExtClass.get("mb3").on("click", new AnonymousDelegate(delegate
			{
				MessageBox.show(new Dictionary(
				   "title", "Address",
				   "msg", "Please enter your address:",
				   "width", 300,
				   "buttons", MessageBox.OKCANCEL,
				   "multiline", true,
				   "fn", new MessageBoxResponseDelegate(ShowResultText),
				   "animEl", "mb3"));
			}));

			ExtClass.get("mb4").on("click", new AnonymousDelegate(delegate
			{
				MessageBox.show(new Dictionary(
				   "title", "Save Changes?",
				   "msg", "You are closing a tab that has unsaved changes. <br />Would you like to save your changes?",
				   "buttons", MessageBox.YESNOCANCEL,
				   "fn", new MessageBoxResponseDelegate(ShowResult),
				   "animEl", "mb4",
				   "icon", MessageBox.QUESTION));
			}));

			ExtClass.get("mb6").on("click", new AnonymousDelegate(delegate
			{
				MessageBox.show(new Dictionary(
				   "title", "Please wait",
				   "msg", "Loading items...",
				   "progressText", "Initializing...",
				   "width", 300,
				   "progress", true,
				   "closable", false,
				   "animEl", "mb6"));

				for (int i = 1; i < 13; i++)
				{
					Window.SetTimeout(GetProgressFunc(i), i*500);
				}
			}));

		    ExtClass.get("mb7").on("click", new AnonymousDelegate(delegate {
		        MessageBox.show(new Dictionary(
		           "msg", "Saving your data, please wait...",
		           "progressText", "Saving...",
		           "width", 300,
		           "wait", true,
		           "waitConfig", new Dictionary("interval",200),
		           "icon", "ext-mb-download", //custom class in msg-box.html
		           "animEl", "mb7"));

				Window.SetTimeout(delegate
				{
					//This simulates a long-running operation like a database save or XHR call.
					//In real code, this would be in a callback function.
					MessageBox.hide();
					MessageBox.alert("Done", "Your fake data was saved!");
				}, 8000);
		    }));

		    ExtClass.get("mb8").on("click", new AnonymousDelegate(delegate
		    {
		        MessageBox.alert("Status", "Changes saved successfully.", new MessageBoxResponseDelegate(ShowResult));
		    }));

		    //Add these values dynamically so they aren"t hard-coded in the html
		    Type.SetField(ExtClass.fly("info").dom, "value", MessageBox.INFO);
		    Type.SetField(ExtClass.fly("question").dom, "value", MessageBox.QUESTION);
		    Type.SetField(ExtClass.fly("warning").dom, "value", MessageBox.WARNING);
		    Type.SetField(ExtClass.fly("error").dom, "value", MessageBox.ERROR);

		    ExtClass.get("mb9").on("click", new AnonymousDelegate(delegate
		    {
		        MessageBox.show(new Dictionary(
		           "title", "Icon Support",
		           "msg", "Here is a message with an icon!",
		           "buttons", MessageBox.OK,
		           "animEl", "mb9",
		           "fn", new MessageBoxResponseDelegate(ShowResult),
		           "icon", Type.GetField(ExtClass.get("icons").dom, "value")
		       ));
		    }));
		}

		private static void ShowResult(string btn, string text)
		{
		    MessageBox.alert("Button Click", String.Format("You clicked the {0} button", btn));
		}

		private static void ShowResultText(string btn, string text)
		{
			MessageBox.alert("Button Click", String.Format("You clicked the {0} button and entered the text \"{1}\".", btn, text));
		}

		private static Callback GetProgressFunc(int i)
		{
			return delegate
				{
					if (i == 12)
					{
						MessageBox.hide();
						MessageBox.alert("Done", "Your fake items were loaded!");
					}
					else
					{
						double v = i/11.0;
						MessageBox.updateProgress(v, Math.Round(100*v) + "% completed");
					}
				};
			
		}
    }
}
